<?php

namespace app\controller;

use app\constants\RsaConstant;
use app\traits\ControllerTrait;


class PhotoAlbum extends ControllerTrait
{
    public function Index()
    {
    }

    /** 获取相册 */
    public function List()
    {
        $param = $this->request->param();
        $rule = [
            "owner|拥有者" => "require",
        ];
        $where = [['owner', '=', $param['owner']], ['access_permission', '=', '1']];
        $this->validate($param, $rule);
        $list = app('photoAlbumModel')
            ::where(
                function ($query) use ($where) {
                    $query->whereOr($where);
                }
            )
            ->field(["uid", "name", "access_permission", "cover", "remark", 'tag', 'assets', 'create_time', 'update_time'])
            ->order('id', 'asc')
            ->select();
        foreach ($list as $key => $assetStr) {
//            echo isset($assetStr['assets']).'分隔符<br/>';
            if (isset($assetStr['assets'])) {
                $keys = array();
                $assetsKeys = explode(",", $assetStr['assets']); // 拆分资源数组
                foreach ($assetsKeys as $index => $assetsUid) {
                    $assetItem = app('imageAssetModel')
                        ::where('uid', $assetsUid)
                        ->field(["uid", "name", "small", "url", "remark", 'create_time', 'update_time'])
                        ->find();

                    $assetItem['small'] = RsaConstant::QI_NIU_DOMAIN . '' . $assetItem['small'];
                    $assetItem['url'] = RsaConstant::QI_NIU_DOMAIN . '' . $assetItem['url'];
                    array_push($keys, $assetItem);
                }
                $assetStr['assets'] = $keys;
            }
        }

        $this->success($list, '相册列表');
    }

    /**
     * 创建一个相册
     */
    public function Create()
    {
        $param = $this->request->param();
        $rule = [
            "name|相册名称" => "require|max:10",
            "owner|拥有者" => "require",
            "authority|权限" => "min:0",
            "cover|封面" => "min:0",
            "remark|备注" => "min:0",
            "tag|标签" => "min:0",
        ];
        $this->validate($param, $rule);
        $album = app('photoAlbumModel');
        $uid = create_guid();
        $album->save([
            'uid' => $uid,
            'name' => $param['name'],
            'owner' => $param['owner'],
            'access_permission' => isset($param['authority']) ? $param['authority'] : '',
            'cover' => isset($param['cover']) ? $param['cover'] : '',
            'remark' => isset($param['remark']) ? $param['remark'] : '',
            'tag' => isset($param['tag']) ? $param['tag'] : '',

        ]);
        $name = $param['name'] .= '相册创建成功';
        $this->success($album, $name);
    }

    /**
     * 向相册中添加图片资源
     */
    public function AddAssets()
    {
        $param = $this->request->param();
        $rule = [
            "uid|相册uid" => "require",
            "assets|资源key" => "require|Array",
        ];
        $this->validate($param, $rule);
        $album = app('photoAlbumModel')
            ::where('uid', $param['uid'])
            ->find();
        $album->save([
            'assets' => isset($album['assets']) ? $album['assets'] . ',' . implode(',', $param['assets']) : implode(',', $param['assets'])
        ]);
        $this->success($album, '资源添加成功');
    }

    public function CleanEmpty()
    {
        $param = $this->request->param();
        $rule = [
            "uid|相册uid" => "require",
        ];
        $this->validate($param, $rule);
        $album = app('photoAlbumModel')
            ::where('uid', $param['uid'])
            ->find();
        $album->save([
            'assets' => null
        ]);
        $this->success($album, '资源清空成功');
    }


}